package logic

import (
	"context"
	"fmt"
	"product-mall/models/mysql"

	"product-mall/productrpc/internal/svc"
	"product-mall/productrpc/productrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddClassLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddClassLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddClassLogic {
	return &AddClassLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddClassLogic) AddClass(in *productrpc.AddClassRequest) (*productrpc.AddClassResponse, error) {
	// todo: add your logic here and delete this line

	var class mysql.Class
	class = mysql.Class{
		UserId: in.UserId,
		Name:   in.Name,
	}
	err := mysql.DB.Debug().Create(&class).Error
	if err != nil {
		return nil, fmt.Errorf("分类添加失败")
	}

	return &productrpc.AddClassResponse{Id: int64(class.ID)}, nil
}
